home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevdjet.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  16KB  |  472 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevdjet.c */
  20. /* HP LaserJet/DeskJet driver for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gdevpcl.h"
  23.  
  24. /*
  25.  * Thanks to Jim Mayer (mayer@wrc.xerox.com),
  26.  * Jan-Mark Wams (jms@cs.vu.nl), Frans van Hoesel (hoesel@rugr86.rug.nl),
  27.  * and George Cameron (g.cameron@biomed.abdn.ac.uk) for improvements.
  28.  */
  29.  
  30. /*
  31.  * You may select a resolution of 75, 100, 150, or 300 DPI.
  32.  * Normally you would do this in the makefile or on the gs command line,
  33.  * not here.
  34.  *
  35.  * If the preprocessor symbol A4 is defined, the default paper size is
  36.  * the European A4 size; otherwise it is the U.S. letter size (8.5"x11").
  37.  *
  38.  * You may find the following test page useful in determining the exact
  39.  * margin settings on your printer.  It prints four big arrows which
  40.  * point exactly to the for corners of an A4 sized paper. Of course the
  41.  * arrows cannot appear in full on the paper, and they are truncated by
  42.  * the margins. The margins measured on the testpage must match those
  43.  * in gdevdjet.c.  So the testpage indicates two facts: 1) the page is
  44.  * not printed in the right position 2) the page is truncated too much
  45.  * because the margins are wrong. Setting wrong margins in gdevdjet.c
  46.  * will also move the page, so both facts should be matched with the
  47.  * real world.
  48.  
  49. %!
  50.     newpath 
  51.     0 0 moveto 144 72 lineto 72 144 lineto
  52.     closepath fill stroke 0 0 moveto 144 144 lineto stroke
  53.  
  54.     595.27 841.88 moveto 451.27 769.88 lineto 523.27 697.88 lineto
  55.     closepath fill stroke 595.27 841.88 moveto 451.27 697.88 lineto stroke
  56.  
  57.     0 841.88 moveto 144 769.88 lineto 72 697.88 lineto
  58.     closepath fill stroke 0 841.88 moveto 144 697.88 lineto stroke
  59.  
  60.     595.27 0 moveto 451.27 72 lineto 523.27 144 lineto
  61.     closepath fill stroke 595.27 0 moveto 451.27 144 lineto stroke
  62.  
  63.     /Helvetica findfont
  64.     14 scalefont setfont
  65.     100 600 moveto
  66.     (This is an A4 testpage. The arrows should point exactly to the) show
  67.     100 580 moveto
  68.     (corners and the margins should match those given in gdev*.c) show
  69.     showpage
  70.  
  71.  */
  72. /*#define X_DPI 300*/
  73. /*#define Y_DPI 300*/
  74.  
  75. #define X_DPI_MAX 300
  76. #define Y_DPI_MAX 300
  77.  
  78. #ifndef X_DPI
  79. #  define X_DPI X_DPI_MAX
  80. #endif
  81. #ifndef Y_DPI
  82. #  define Y_DPI Y_DPI_MAX
  83. #endif
  84.  
  85. /*
  86.  * For all DeskJet Printers:
  87.  *
  88.  *  Maximum printing width               = 2400 dots = 8"
  89.  *  Maximum recommended printing height  = 3100 dots = 10 1/3"
  90.  *
  91.  * All Deskjets have 1/2" unprintable bottom margin.
  92.  * The recommendation comes from the HP Software Developer's Guide for
  93.  * the DeskJet 500, DeskJet PLUS, and DeskJet printers, version C.01.00
  94.  * of 12/1/90.
  95.  *
  96.  * Note that the margins defined just below here apply only to the DeskJet;
  97.  * the paper size, width and height apply to the LaserJet as well.
  98.  */
  99.  
  100. /* Margins are left, bottom, right, top. */
  101. /* from Frans van Hoesel hoesel@rugr86.rug.nl. */
  102. /* A4 has a left margin of 1/8 inch and at a printing width of
  103.  * 8 inch this give a right margin of 0.143. The 0.09 top margin is
  104.  * not the actual margin - which is 0.07 - but compensates for the
  105.  * inexact paperlength which is set to 117 10ths 
  106.  * Somebody should check for letter sized paper. I left it at 0.07
  107.  */
  108. #define DESKJET_MARGINS_LETTER  0.25, 0.50, 0.25, 0.07
  109. #define DESKJET_MARGINS_A4      0.125, 0.5, 0.143, 0.09
  110. /* Similar margins for the LaserJet, */
  111. /* from Eddy Andrews eeandrew@pyr.swan.ac.uk. */
  112. #define LASERJET_MARGINS_A4    0.25, 0.20, 0.25, 0.00
  113. #define LASERJET_MARGINS_LETTER    0.35, 0.20, 0.35, 0.00
  114.  
  115. #ifndef A4
  116. #  define DESKJET_MARGINS    DESKJET_MARGINS_LETTER
  117. #  define LASERJET_MARGINS    LASERJET_MARGINS_LETTER
  118. #else
  119. #  define DESKJET_MARGINS    DESKJET_MARGINS_A4
  120. #  define LASERJET_MARGINS    LASERJET_MARGINS_A4
  121. #endif
  122.  
  123. /* The number of blank lines that make it worthwhile to reposition */
  124. /* the cursor. */
  125. #define MIN_SKIP_LINES 7
  126.  
  127. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  128. #define W sizeof(word)
  129. #define LINE_SIZE ((X_DPI_MAX * 85 / 10 + W * 8 - 1) / (W * 8) * W)
  130.  
  131. /* Printer types */
  132. #define LJ    0
  133. #define LJplus    1
  134. #define LJ2p    2
  135. #define LJ3    3
  136. #define DJ    4
  137. #define DJ500    5
  138.  
  139. /*
  140.  * The notion that there is such a thing as a "PCL printer" is a fiction:
  141.  * no two "PCL" printers, even at the same PCL level, have compatible
  142.  * command sets.  The command strings below were established by hearsay
  143.  * and by trial and error.  (The H-P documentation isn't fully accurate
  144.  * either; for example, it doesn't reveal that the DeskJet printers
  145.  * implement anything beyond PCL 3.)
  146.  */
  147.  
  148. /* Printer capabilities */
  149. typedef enum {
  150.     mode_0,        /* PCL 3, use <ESC>*p+<n>Y for vertical spacing */
  151.     mode_2,        /* PCL 4, use <ESC>*b<n>Y for vertical spacing */
  152.     mode_2p,    /* PCL 4 but no vertical spacing */
  153.     mode_3        /* PCL 5, use <ESC>*b<n>Y and clear seed row */
  154.             /* (includes mode 2) */
  155. } compression_modes;
  156.  
  157. /* The printer initialization strings. */
  158. private const char *init_strings[] = {
  159.     /* LaserJet PCL 3, no compression */
  160.         "\033*p0x0Y\033*b0M",
  161.     /* LaserJet Plus PCL 3, no compression */
  162.         "\033*p0x0Y\033*b0M",
  163.     /* LaserJet IIP PCL 4, mode 2 compression */
  164.         "\033*r0F\033*p0x75Y\033*b2M",
  165.     /* LaserJet III PCL 5, mode 2&3 compression */
  166.         "\033*r0F\033*p0x75Y",
  167.     /* DeskJet almost PCL 4, mode 2 compression */
  168.         "\033&k1W\033*p0x0Y\033*b2M",
  169.     /* DeskJet 500 almost PCL 4, mode 2&3 compression */
  170.         "\033&k1W\033*p0x0Y",
  171. };
  172.  
  173. /* The device descriptors */
  174. private dev_proc_open_device(hpjet_open);
  175. private dev_proc_print_page(djet_print_page);
  176. private dev_proc_print_page(djet500_print_page);
  177. private dev_proc_print_page(ljet_print_page);
  178. private dev_proc_print_page(ljetplus_print_page);
  179. private dev_proc_print_page(ljet2p_print_page);
  180. private dev_proc_print_page(ljet3_print_page);
  181.  
  182. gx_device_procs prn_hp_procs =
  183.   prn_matrix_procs(hpjet_open, gdev_pcl_get_initial_matrix,
  184.     gdev_prn_output_page, gdev_prn_close);
  185.  
  186. gx_device_printer far_data gs_deskjet_device =
  187.   prn_device(prn_hp_procs, "deskjet",
  188.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  189.     X_DPI, Y_DPI,
  190.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  191.     1, djet_print_page);
  192.  
  193. gx_device_printer far_data gs_djet500_device =
  194.   prn_device(prn_hp_procs, "djet500",
  195.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  196.     X_DPI, Y_DPI,
  197.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  198.     1, djet500_print_page);
  199.  
  200. gx_device_printer far_data gs_laserjet_device =
  201.   prn_device(prn_hp_procs, "laserjet",
  202.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  203.     X_DPI, Y_DPI,
  204.     0.05, 0.25, 0.55, 0.25,        /* margins */
  205.     1, ljet_print_page);
  206.  
  207. gx_device_printer far_data gs_ljetplus_device =
  208.   prn_device(prn_hp_procs, "ljetplus",
  209.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  210.     X_DPI, Y_DPI,
  211.     0.05, 0.25, 0.55, 0.25,        /* margins */
  212.     1, ljetplus_print_page);
  213.  
  214. gx_device_printer far_data gs_ljet2p_device =
  215.   prn_device(prn_hp_procs, "ljet2p",
  216.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  217.     X_DPI, Y_DPI,
  218.     0.20, 0.25, 0.25, 0.25,        /* margins */
  219.     1, ljet2p_print_page);
  220.  
  221. gx_device_printer far_data gs_ljet3_device =
  222.   prn_device(prn_hp_procs, "ljet3",
  223.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  224.     X_DPI, Y_DPI,
  225.     0.20, 0.25, 0.25, 0.25,        /* margins */
  226.     1, ljet3_print_page);
  227.  
  228. /* Forward references */
  229. private int hpjet_print_page(P4(gx_device_printer *, FILE *, int, compression_modes));
  230.  
  231. /* Open the printer, adjusting the margins if necessary. */
  232. private int
  233. hpjet_open(gx_device *pdev)
  234. {    /* Change the margins if necessary. */
  235.     const float _ds *m;
  236. #define ppdev ((gx_device_printer *)pdev)
  237.     if ( ppdev->print_page == djet_print_page ||
  238.          ppdev->print_page == djet500_print_page
  239.        )
  240. #undef ppdev
  241.     {    static const float m_a4[4] = { DESKJET_MARGINS_A4 };
  242.         static const float m_letter[4] = { DESKJET_MARGINS_LETTER };
  243.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  244.             m_letter);
  245.     }
  246.     else    /* LaserJet */
  247.     {    static const float m_a4[4] = { LASERJET_MARGINS_A4 };
  248.         static const float m_letter[4] = { LASERJET_MARGINS_LETTER };
  249.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  250.             m_letter);
  251.     }
  252.     pdev->l_margin = m[0];
  253.     pdev->b_margin = m[1];
  254.     pdev->r_margin = m[2];
  255.     pdev->t_margin = m[3];
  256.     return gdev_prn_open(pdev);
  257. }
  258.  
  259. /* ------ Internal routines ------ */
  260.  
  261. /* The DeskJet can compress (mode 2) */
  262. private int
  263. djet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  264. {    return hpjet_print_page(pdev, prn_stream, DJ, mode_2);
  265. }
  266. /* The DeskJet500 can compress (modes 2&3) */
  267. private int
  268. djet500_print_page(gx_device_printer *pdev, FILE *prn_stream)
  269. {    return hpjet_print_page(pdev, prn_stream, DJ500, mode_3);
  270. }
  271. /* The LaserJet series II can't compress */
  272. private int
  273. ljet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  274. {    return hpjet_print_page(pdev, prn_stream, LJ, mode_0);
  275. }
  276. /* The LaserJet Plus can't compress */
  277. private int
  278. ljetplus_print_page(gx_device_printer *pdev, FILE *prn_stream)
  279. {    return hpjet_print_page(pdev, prn_stream, LJplus, mode_0);
  280. }
  281. /* All LaserJet series IIIs (III,IIId,IIIp,IIIsi) compress (modes 2&3) */
  282. private int
  283. ljet3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  284. {    return hpjet_print_page(pdev, prn_stream, LJ3, mode_3);
  285. }
  286. /* LaserJet series IIp & IId compress (mode 2) */
  287. /* but don't support *p+ or *b vertical spacing. */
  288. private int
  289. ljet2p_print_page(gx_device_printer *pdev, FILE *prn_stream)
  290. {    return hpjet_print_page(pdev, prn_stream, LJ2p, mode_2p);
  291. }
  292.  
  293. /* Send the page to the printer.  For speed, compress each scan line, */
  294. /* since computer-to-printer communication time is often a bottleneck. */
  295. private int
  296. hpjet_print_page(gx_device_printer *pdev, FILE *prn_stream, int ptype,
  297.   compression_modes cmodes)
  298. {    int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  299.     int line_size_words = (line_size + W - 1) / W;
  300.     uint storage_size_words = line_size_words * 8; /* data, out_row, out_row_alt, prev_row */
  301.     word *storage = (ulong *)gs_malloc(storage_size_words, W,
  302.                        "hpjet_print_page");
  303.     word
  304.       *data_words,
  305.       *out_row_words,
  306.       *out_row_alt_words,
  307.       *prev_row_words;
  308. #define data ((char *)data_words)
  309. #define out_row ((char *)out_row_words)
  310. #define out_row_alt ((char *)out_row_alt_words)
  311. #define prev_row ((char *)prev_row_words)
  312.     char *out_data;
  313.     int x_dpi = pdev->x_pixels_per_inch;
  314.     int y_dots_per_pixel = Y_DPI_MAX / pdev->y_pixels_per_inch;
  315.     int out_count;
  316.     int compression = -1;
  317.     static const char *from2to3 = "\033*b3M";
  318.     static const char *from3to2 = "\033*b2M";
  319.     int penalty_from2to3 = strlen(from2to3);
  320.     int penalty_from3to2 = strlen(from3to2);
  321.     int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  322.  
  323.     if ( storage == 0 )    /* can't allocate working area */
  324.         return_error(gs_error_VMerror);
  325.     data_words = storage;
  326.     out_row_words = data_words + (line_size_words * 2);
  327.     out_row_alt_words = out_row_words + (line_size_words * 2);
  328.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  329.     /* Clear temp storage */
  330.     memset(data, 0, storage_size_words * W);
  331.  
  332.     /* Initialize printer. */
  333.     fputs("\033E", prn_stream);        /* reset printer */
  334.     fputs("\033*rB", prn_stream);        /* end raster graphics */
  335.     fprintf(prn_stream, "\033*t%dR", x_dpi);    /* set resolution */
  336.     /* If the printer supports it, set the paper size */
  337.     /* based on the actual requested size. */
  338.     if ( !(ptype == LJ || ptype == LJplus ) )
  339.     {    fprintf(prn_stream, "\033&l%dA", paper_size);
  340.     }
  341.     fputs("\033&l0o0e0L", prn_stream);
  342.     fputs(init_strings[ptype], prn_stream);
  343.  
  344.     /* Send each scan line in turn */
  345.        {    int lnum;
  346.         int num_blank_lines = 0;
  347.         word rmask = ~(word)0 << (-pdev->width & (W * 8 - 1));
  348.  
  349.         /* Transfer raster graphics. */
  350.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  351.            {    register word *end_data = data_words + line_size_words;
  352.             gdev_prn_copy_scan_lines(pdev, lnum,
  353.                          (byte *)data, line_size);
  354.                /* Mask off 1-bits beyond the line width. */
  355.             end_data[-1] &= rmask;
  356.             /* Remove trailing 0s. */
  357.             while ( end_data > data_words && end_data[-1] == 0 )
  358.               end_data--;
  359.             if ( end_data == data_words )
  360.                {    /* Blank line */
  361.                 num_blank_lines++;
  362.                 continue;
  363.                }
  364.  
  365.             /* We've reached a non-blank line. */
  366.             /* Put out a spacing command if necessary. */
  367.             if ( num_blank_lines == lnum )
  368.             {    /* We're at the top of a page. */
  369.                 if ( cmodes == mode_2p )
  370.                 {    /* Start raster graphics. */
  371.                     fputs("\033*r1A", prn_stream);
  372.                     for ( ; num_blank_lines; num_blank_lines-- )
  373.                         fputs("\033*bW", prn_stream);
  374.                 }
  375.                 else
  376.                 {    if ( num_blank_lines > 0 )
  377.                       fprintf(prn_stream, "\033*p+%dY",
  378.                         num_blank_lines * y_dots_per_pixel);
  379.                     /* Start raster graphics. */
  380.                     fputs("\033*r1A", prn_stream);
  381.                 }
  382.             }
  383.             /* Skip blank lines if any */
  384.             else if ( num_blank_lines != 0 )
  385.             {  if ( num_blank_lines < MIN_SKIP_LINES || cmodes == mode_2p )
  386.                {    /* Moving down from current position */
  387.                 /* causes head motion on the DeskJet, so */
  388.                 /* if the number of lines is small, */
  389.                 /* we're better off printing blanks. */
  390.                 if ( cmodes == mode_3 )
  391.                 {    /* Must clear the seed row. */
  392.                     fputs("\033*bY", prn_stream);
  393.                 }
  394.                 for ( ; num_blank_lines; num_blank_lines-- )
  395.                     fputs("\033*bW", prn_stream);
  396.                }
  397.                else if ( cmodes == mode_0 )    /* PCL 3 */
  398.                {    fprintf(prn_stream, "\033*p+%dY",
  399.                         num_blank_lines * y_dots_per_pixel);
  400.                }
  401.                else
  402.                {       fprintf(prn_stream, "\033*b%dY",
  403.                        num_blank_lines);
  404.                }
  405.                /* Clear the seed row (only matters for */
  406.                /* mode 3 compression). */
  407.                memset(prev_row, 0, line_size);
  408.             }
  409.             num_blank_lines = 0;
  410.  
  411.             /* Choose the best compression mode */
  412.             /* for this particular line. */
  413.             switch (cmodes)
  414.               {
  415.               case mode_3:
  416.                {    /* Compression modes 2 and 3 are both */
  417.                 /* available.  Try both and see which one */
  418.                 /* produces the least output data. */
  419.                 int count3 = gdev_pcl_mode3compress(line_size, data,
  420.                                prev_row, out_row);
  421.                 int count2 = gdev_pcl_mode2compress(data_words, end_data,
  422.                                out_row_alt);
  423.                 int penalty3 =
  424.                   (compression == 3 ? 0 : penalty_from2to3);
  425.                 int penalty2 =
  426.                   (compression == 2 ? 0 : penalty_from3to2);
  427.                 if ( count3 + penalty3 < count2 + penalty2)
  428.                    {    if ( compression != 3 )
  429.                         fputs(from2to3, prn_stream);
  430.                     compression = 3;
  431.                     out_data = out_row;
  432.                     out_count = count3;
  433.                    }
  434.                 else
  435.                    {    if ( compression != 2 )
  436.                         fputs(from3to2, prn_stream);
  437.                     compression = 2;
  438.                     out_data = out_row_alt;
  439.                     out_count = count2;
  440.                    }
  441.                 break;
  442.                }
  443.               case mode_2:
  444.               case mode_2p:
  445.                 out_data = out_row;
  446.                    out_count = gdev_pcl_mode2compress(data_words, end_data,
  447.                               out_row);
  448.                 break;
  449.               default:
  450.                 out_data = data;
  451.                 out_count = (char *)end_data - data;
  452.               }
  453.  
  454.             /* Transfer the data */
  455.             fprintf(prn_stream, "\033*b%dW", out_count);
  456.             fwrite(out_data, sizeof(char), out_count,
  457.                    prn_stream);
  458.            }
  459.     }
  460.  
  461.     /* end raster graphics */
  462.     fputs("\033*rB", prn_stream);
  463.  
  464.     /* eject page */
  465.     fputs("\033&l0H", prn_stream);
  466.  
  467.     /* free temporary storage */
  468.     gs_free((char *)storage, storage_size_words, W, "hpjet_print_page");
  469.  
  470.     return 0;
  471. }
  472.